package routes

import (
	"github.com/garyburd/redigo/redis"
	"github.com/martini-contrib/render"
	"log"
	"net/http"
	"rhelper"
)

type AccountEditValue struct {
	Edit     bool   `json:"-"`
	Error    string `json:"err,omitempty"`
	Id       string `form:"textinput-accountId" json:"id,omitempty"`
	Password string `form:"textinput-password" json:"password,omitempty"`
	Mobile   string `form:"textinput-mobile" json:"mobile,omitempty"`
}

func GetAccMgr(r render.Render, req *http.Request) {
	var a = req.URL.Query().Get("a")
	conn := rhelper.Pool().Get()
	defer conn.Close()

	keys, err := redis.Strings(conn.Do("KEYS", rhelper.AccountHash+"*"))
	if err != nil {
		log.Println(err)
		keys = []string{}
	}

	var accounts = make([]string, len(keys))
	for i, key := range keys {
		accounts[i] = key[len(rhelper.AccountHash):]
	}

	r.HTML(200, "accMgr", TmplValue{Title: "Account manange", ExtValue: accounts, ExtValue2: a})
}

func EditAcc(r render.Render, req *http.Request) {
	var flag = req.URL.Query().Get("a")
	if flag == "newAccount" {
		var dummyAccountEditValue = AccountEditValue{Edit: false}
		r.HTML(200, "accEdit", TmplValue{Title: "Create account",
			ExtValue: dummyAccountEditValue})
	} else {
		var dummyAccountEditValue = AccountEditValue{Edit: true}
		var account = flag
		conn := rhelper.Pool().Get()
		defer conn.Close()

		fields, err := redis.Strings(conn.Do("HMGET", rhelper.AccountHash+account,
			"password", "mobile"))

		if err == nil {
			dummyAccountEditValue.Id = account
			dummyAccountEditValue.Password = fields[0]
			dummyAccountEditValue.Mobile = fields[1]
		} else {
			log.Println(err)
		}

		r.HTML(200, "accEdit", TmplValue{Title: "Edit account",
			ExtValue: dummyAccountEditValue})
	}

}

func CheckAccountExist(req *http.Request) string {
	var account = req.URL.Query().Get("textinput-accountId")
	if account == "" {
		return "account must specify"
	}

	conn := rhelper.Pool().Get()
	defer conn.Close()

	p, err := redis.String(conn.Do("HGET", rhelper.AccountHash+account, "password"))
	if err != nil {
		log.Println(err)
	}

	if p != "" {
		return "account already exist"
	}

	return "true"
}

func SaveAccount(accountValue AccountEditValue) string {

	var account = accountValue.Id
	var password = accountValue.Password
	var mobile = accountValue.Mobile

	conn := rhelper.Pool().Get()
	defer conn.Close()

	_, err := conn.Do("HMSET", rhelper.AccountHash+account,
		"password", password, "mobile", mobile)

	if err != nil {
		log.Println(err)
		return err.Error()
	}

	return "ok"
}

func GetAccountInfo(r render.Render, req *http.Request) {
	var account = req.URL.Query().Get("accountId")

	conn := rhelper.Pool().Get()
	defer conn.Close()

	var dummyAccountEditValue = AccountEditValue{Edit: false}
	dummyAccountEditValue.Error = ""

	fields, err := redis.Strings(conn.Do("HMGET", rhelper.AccountHash+account,
		"password", "mobile"))
	if err != nil {
		dummyAccountEditValue.Error = err.Error()
		r.JSON(200, dummyAccountEditValue)
		return
	}

	dummyAccountEditValue.Id = account
	dummyAccountEditValue.Password = fields[0]
	dummyAccountEditValue.Mobile = fields[1]

	r.JSON(200, dummyAccountEditValue)
}

func DeleteAccount(req *http.Request) string {
	err := req.ParseForm()
	if err != nil {
		log.Println(err)
		return err.Error()
	}

	var account = req.Form.Get("id")

	if account == "" {
		return "please select an account to delete"
	}

	conn := rhelper.Pool().Get()
	defer conn.Close()

	_, err = conn.Do("DEL", rhelper.AccountHash+account)
	if err != nil {
		return err.Error()
	}

	return "ok"
}
